//
//  PictureCell.m
//  Mooker
//
//  Created by hua zhang on 3/12/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PictureCell.h"
#import "PicModel.h"

@interface PictureCell ()

@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIImageView *numImage;
@property (nonatomic, retain) UILabel     *numLabel;

@end

@implementation PictureCell

- (void)dealloc {
    RELEASE_SAFELY(_picture);
    RELEASE_SAFELY(_imageView);
    RELEASE_SAFELY(_numImage);
    RELEASE_SAFELY(_numLabel);
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:CGRectMake(0, 0, kDefaultPictureCellWidth, kDefaultPictureCellHeight)];
    if (self) 
    {
        
    }
    return self;
}

- (void)prepareForReuse 
{
    [super prepareForReuse];
//    RELEASE_SAFELY(_picture);
}

- (void)setPicture:(PicModel *)picture{
    if (_picture != picture) {
        _picture = picture;
        NSString *url = nil;
        if ([_picture.SinglePics count] > 0)
            {
            url = _picture.thumal;
            __block UIImageView *wImageView = self.imageView;
            [self.imageView setImageWithURL:[NSURL URLWithString:url] placeholderImage:IMGNAMED(@"bg_pic_placeholder_red.png") completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                if (image) {

                    CABasicAnimation *opAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
                    opAnim.duration = .25;
                    opAnim.fromValue = [NSNumber numberWithFloat:0.0];
                    opAnim.toValue = [NSNumber numberWithFloat:1.0];
                    opAnim.removedOnCompletion = YES;
                    [opAnim setValue:@"fadeInAnim" forKey:@"moveAnim"];
                    opAnim.fillMode = kCAFillModeForwards;

                    [wImageView.layer addAnimation:opAnim forKey:@"moveAnim"];
                }

            }];
            self.numLabel.text = [NSString stringWithFormat:@"%d",[_picture.SinglePics count]];
            }
    }
}
- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        _imageView.backgroundColor = [UIColor clearColor];
        _imageView.clipsToBounds = YES;
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        [_imageView addSubview:self.numImage];
        [_imageView addSubview:self.numLabel];
        [self addSubview:_imageView];
    }
    return _imageView;
}

- (UIImageView *)numImage
{
    if (!_numImage)
    {
        _numImage = [[UIImageView alloc] initWithImage:IMGNAMED(@"bg_imagecount.png")];
        _numImage.frame = CGRectMake(self.bounds.size.width - 20,
                                     self.bounds.size.height - 20,
                                     15,
                                     15);
    }
    return _numImage;
}

- (UILabel *)numLabel
{
    if (!_numLabel)
    {
        _numLabel = [[UILabel alloc] initWithFrame:self.numImage.frame];
        _numLabel.backgroundColor = [UIColor clearColor];
        _numLabel.font = FONT_NUMBER(11.0f);
        _numLabel.textAlignment = NSTextAlignmentCenter;
        _numLabel.textColor = [UIColor whiteColor];
    }
    return _numLabel;
}
@end
